package com.jar.exercise.网络编程.UDP连接.demo1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Sender {


	public static void main(String[] args) {
		BufferedReader br = null;
		DatagramSocket ds = null;
		//

		String line = null;
		try {
			//创建一个从控制台输入的字符流（字节流转换成字符流）
			br = new BufferedReader(new InputStreamReader(System.in));
			//创建我们udp的socket
			ds = new DatagramSocket();
			//不断的从字符流中读取数据
			while ((line = br.readLine()) != null) {
				//把读到的每一行变成字节数组
				byte[] bs = line.getBytes();
				//给数据打包
				DatagramPacket dp = new DatagramPacket(bs, bs.length, InetAddress.getByName("192.168.137.1"), 10000);
				//发送数据
				ds.send(dp);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (ds != null) {
				ds.close();
			}
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
